package frontend.model.worklog;

import backend.model.admin.*;
import backend.model.worklog.WorkLog;
import frontend.core.TimeTrackingWebSession;
import frontend.model.base.UIModel;
import org.apache.commons.lang.time.DateUtils;

import java.util.Calendar;
import java.util.Date;

public class WorkLogUIModel implements UIModel<WorkLog> {

    private WorkLog workLog = new WorkLog();

    private Team team;
    private Employee employee;
    private Project project;
    private Sfi sfi;
    private Workplace workplace;
    private WorkDetail workDetail;
    private Shift shift;

    private Date date;
    private String hours = String.valueOf(0);

    public WorkLogUIModel() {
    }

    public WorkLogUIModel(WorkLog workLog) {
        this.workLog = workLog;
        populateFieldsFromDBModel();
    }

    public WorkLogUIModel(Employee employee) {
        this.employee = employee;
    }

    @Override
    public WorkLog getDBModel() {
        return workLog;
    }

    public Employee getEmployee() {
        return employee;
    }

    public Date getDate() {
        return date;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public Workplace getWorkplace() {
        return workplace;
    }

    public void setWorkplace(Workplace workplace) {
        this.workplace = workplace;
    }

    public WorkDetail getWorkDetail() {
        return workDetail;
    }

    public void setWorkDetail(WorkDetail workDetail) {
        this.workDetail = workDetail;
    }

    public void resetWorkLog() {
        this.workLog = new WorkLog();
    }

    public Sfi getSfi() {
        return sfi;
    }

    public  void setSfi(Sfi sfi) {
        this.sfi = sfi;
    }

    public Shift getShift() {
        return shift;
    }

    public void setShift(Shift shift) {
        this.shift = shift;
    }

    public String getHours() {
        return hours;
    }

    public void setHours(int hours) {
        this.hours = String.valueOf(hours);
    }

    @Override
    public void populateDBModelFromFields() {
        Date date = TimeTrackingWebSession.getSession().getSelectedDay().getTime();
        workLog.setDate(DateUtils.truncate(date, Calendar.DATE));
        workLog.setEmployee(employee);
        workLog.setOccupation(employee.getOccupation());
        workLog.setForeman(employee.getTeam().getForeman());
        workLog.setTeam(employee.getTeam());
        workLog.setProject(project);
        int bookedHours = Integer.valueOf(hours);
        if (project.getIsDirectPayment()) {
            workLog.setSfi(sfi);
            workLog.setWorkplace(workplace);
            workLog.setWorkDetail(workDetail);
            workLog.setShift(shift);
        } else {
            workLog.setSfi(null);
            workLog.setWorkDetail(null);
            workLog.setWorkplace(null);
            workLog.setShift(null);
            bookedHours = 0;
        }
        workLog.setHours(bookedHours);
    }

    @Override
    public void populateFieldsFromDBModel() {
        employee = workLog.getEmployee();
        date = workLog.getDate();
        project = workLog.getProject();
        sfi = workLog.getSfi();
        workplace = workLog.getWorkplace();
        workDetail = workLog.getWorkDetail();
        shift = workLog.getShift();
        hours = String.valueOf(workLog.getHours());
    }
}
